/* eslint-disable */
var monthDay = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
/*判断某年是否是闰年*/
function isLeap(year) {
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
		return true;
	} else {
		return false;
	}
}
/*判断某年某月某日是星期几，默认日为1号*/
function whatDay(year, month, day = 1) {
	var sum = 0;
	sum += (year - 1) * 365 + Math.floor((year - 1) / 4) - Math.floor((year - 1) / 100) + Math.floor((year - 1) / 400) +
		day;
	for (var i = 0; i < month - 1; i++) {
		sum += monthDay[i];
	}
	if (month > 2) {
		if (isLeap(year)) {
			sum += 29;
		} else {
			sum += 28;
		}
	}
	return sum % 7 - 1; //余数为0代表那天是周日，为1代表是周一，以此类推
}
/*显示日历*/
function showCld(year, month, firstDay) {
	var i;
	var tagClass = "";
	var nowDate = new Date();

	var days; //从数组里取出该月的天数
	if (month == 2) {
		if (isLeap(year)) {
			days = 29;
		} else {
			days = 28;
		}
	} else {
		days = monthDay[month - 1];
	}

	// 上个月 天数
	var pre_Month
	if (month == 1) {
		pre_Month = 12
	} else {
		pre_Month = month - 1
	}

	var pre_Month_days

	if (pre_Month == 2) {
		if (isLeap(year)) {
			pre_Month_days = 29;
		} else {
			pre_Month_days = 28;
		}
	} else {
		pre_Month_days = monthDay[pre_Month - 1];
	}
	// 计算前面空格
	var dateData = []
	for (let i = 0; i < firstDay; i++) {
		dateData.unshift({
			mon: pre_Month,
			day: pre_Month_days - i
		})
	}

	for (let i = 1; i <= days; i++) {
		dateData.push({
			mon: month,
			day: i
		})
	}
	
	
	let endArr = 7 - dateData.length % 7
	
	if(endArr != 7) {
		var next_Month = month
		if (month == 12) {
			next_Month = 1
		} else {
			next_Month = next_Month + 1
		}
		
		for (let i = 1; i <= endArr; i++) {
			dateData.push({
				mon: next_Month,
				day: i
			})
		}
	}

	return dataHandling(dateData)
}

function dataHandling(arr) {
	console.log(arr)
	let arrLength = arr.length; // 数组长度
	let num = 7; // 每页显示 7 条
	let index = 0;
	let newArr = []
	for (let i = 0; i <= arrLength; i++) {
		if (i % num === 0 && i !== 0) { // 可以被 7 整除
			newArr.push(arr.slice(index, i));
			index = i;
		};
		// if ((i + 1) === arrLength) {
		// 	 newArr.push(arr.slice(index, (i + 1)));
		// }
	};
	return newArr
}

export { whatDay, showCld };
